﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PortChat
{
    /* for checking a checkword */
    class CheckWordChecker
    {
        private Char[] charArrCheckWord = { '0', '0', '0', '0', '0', '0', '0', '0' };

        //List<String> lstCodeWords = new List<String>();
        public void computeWrongDigit(List<String> lstCodeWords , String strCheckWord )
        {
            foreach (String myCodeWord in lstCodeWords)
            {
                computeCheckWord(myCodeWord);                
            }
        }

        //computes the Antivalenz operation of two strings
        //0 XOR 0 -> 0
        //0 XOR 1 -> 1
        //1 XOR 0 -> 1
        //1 XOR 1 -> 0
        private char getAntivalenz(char strWord1P, char strWord2P)
        {
            Char charAntivalenz = '0';
            if (strWord1P.Equals('0') && strWord2P.Equals('0'))
            {
                charAntivalenz = '0';
            }
            if (strWord1P.Equals('0') && strWord2P.Equals('1'))
            {
                charAntivalenz = '1';
            }
            if (strWord1P.Equals('1') && strWord2P.Equals('0'))
            {
                charAntivalenz = '1';
            }
            if (strWord1P.Equals('1') && strWord2P.Equals('1'))
            {
                charAntivalenz = '0';
            }
            return charAntivalenz;
        }

        //computes check word for input code word
        public void computeCheckWord(String strCodeWordP)
        {
            Char[] charArrCodeWord = strCodeWordP.ToCharArray();
            //charArrCheckWord = strCheckWord.ToCharArray();
            charArrCheckWord[0] = getAntivalenz(charArrCheckWord[0], charArrCodeWord[0]);
            charArrCheckWord[1] = getAntivalenz(charArrCheckWord[1], charArrCodeWord[1]);
            charArrCheckWord[2] = getAntivalenz(charArrCheckWord[2], charArrCodeWord[2]);
            charArrCheckWord[3] = getAntivalenz(charArrCheckWord[3], charArrCodeWord[3]);
            charArrCheckWord[4] = getAntivalenz(charArrCheckWord[4], charArrCodeWord[4]);
            charArrCheckWord[5] = getAntivalenz(charArrCheckWord[5], charArrCodeWord[5]);
            charArrCheckWord[6] = getAntivalenz(charArrCheckWord[6], charArrCodeWord[6]);
            charArrCheckWord[7] = getAntivalenz(charArrCheckWord[7], charArrCodeWord[7]);
        }

        public String getCheckWord()
        {
            String strCheckWord = "";
            foreach (char myChar in charArrCheckWord)
            {
                strCheckWord = strCheckWord + myChar;
            }         
            return strCheckWord;
        }

        public void initializeCheckWord()
        {
            for (int i = 0; i < charArrCheckWord.Length; i++)
            {
                charArrCheckWord[i] = '0';
            }
        }

        //if -1 then it is occurred more than one failure
        public int getDiffentDigitPosition(String strCheckWord1, String strCheckWord2)
        {
            char[] charCheckWord1 = strCheckWord1.ToCharArray();
            char[] charCheckWord2 = strCheckWord2.ToCharArray();
            int positionOfError = 0;
            int numberOfFailures = 0;
            for(int i=0; i<charCheckWord1.Length; i++)
            {
                if(!charCheckWord1[i].Equals(charCheckWord2[i]))
                {
                    positionOfError = i;
                    numberOfFailures++;
                }
            }
            if (numberOfFailures > 1)
            {
                positionOfError = -1;
            }
            return positionOfError;
        }
    }
}
